//
//  ByMyCouponsVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/26.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByMyCouponsVC.h"

// cell
#import "ByCellMyCoupons.h"

// view
#import "ByViewMyCouponsSectionHeader.h"

@interface ByMyCouponsVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *items1;
@property (nonatomic, strong) NSMutableArray *items2;
@property (nonatomic, strong) NSMutableArray *items3;

@end

@implementation ByMyCouponsVC

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的优惠券";
    
    [self addLeftBackBarButtonItem];
    [self addBYItemviews:@[@"搜索"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    
    [self createTableView];
    
    [self getCouponsList];
}

#pragma mark - 获取数据
- (void)getCouponsList {
    
    ByModelCoupons *model1 = [ByModelCoupons new];
    model1.money = 88;
    model1.remainDay = 3;
    model1.status = ByCouponsStatusAvailable;
    
    ByModelCoupons *model2 = [ByModelCoupons new];
    model2.money = 99;
    model2.remainDay = 22;
    model2.status = ByCouponsStatusUsed;
    
    ByModelCoupons *model3 = [ByModelCoupons new];
    model3.money = 222;
    model3.remainDay = 5;
    model3.status = ByCouponsStatusExpired;
    
    
    [self.items1 addObject:model1];
    [self.items2 addObject:model2];
    [self.items3 addObject:model3];
    
}

#pragma mark - 懒加载
- (NSMutableArray *)items1 {
    if (!_items1) {
        _items1 = [NSMutableArray new];
    }
    return _items1;
}
- (NSMutableArray *)items2 {
    if (!_items2) {
        _items2 = [NSMutableArray new];
    }
    return _items2;
}
- (NSMutableArray *)items3 {
    if (!_items3) {
        _items3 = [NSMutableArray new];
    }
    return _items3;
}


#pragma mark - 创建视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            return self.items1.count;
            break;
        case 1:
            return self.items2.count;
            break;
        case 2:
            return self.items3.count;
            break;
        default:
            return 0;
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellMyCoupons *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ByCellMyCoupons alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    switch (indexPath.section) {
        case 0:
            cell.model = self.items1[indexPath.row];
            break;
        case 1:
            cell.model = self.items2[indexPath.row];
            break;
        case 2:
            cell.model = self.items3[indexPath.row];
            break;
        default:
            break;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    ByViewMyCouponsSectionHeader *header = [[ByViewMyCouponsSectionHeader alloc] init];
    header.status = section;
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    
    if (indexPath.section == 0) {
        if (self.didSelectCoupons) {
            self.didSelectCoupons(self.items1[indexPath.row]);
            [self dismissViewControllerAnimated:true completion:nil];
        }
    }
    
}
@end
